<?php

namespace Common\Model;

use Think\Model;

import('Common.Lib.ModelTrait'); // 包含代码复制
class BaseModel extends Model
{
    use \ModelTrait; // 继承扩展方法

    public function _initialize()
    {
    }

    /**
     * 快速删除
     * @param $map
     * @return mixed
     */
    public function quickDel($map)
    {
        return $this->where($map)->delete();
    }

    protected function _after_find(&$result, $options)
    {
        $this->checkSerializeField($result);
        if ($this->num2text) {
            foreach ($this->num2text as $key => $v) {
                if (!isset($result[$key])) {
                    continue;
                }
                if (is_string($v) && function_exists($v)) {
                    // 函数
                    $result[$key . '_text'] = $v($result[$key]);
                } else if (is_callable($v)) {
                    // 闭包函数
                    $result[$key . '_text'] = $v($result[$key]);
                } else if (is_array($v)) {
                    // 键值碰撞
                    $result[$key . '_text'] = $v[$result[$key]];
                }
            }
        }
    }

    protected function _after_select(&$resultSet, $options)
    {
        foreach ($resultSet as &$row) {
            $this->_after_find($row, $options);
        }
    }
}
